package com.example.lotterysystem.controller;

import com.example.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import com.example.lotterysystem.common.exception.ControllerException;
import com.example.lotterysystem.common.pojo.CommonResult;
import com.example.lotterysystem.common.utils.JacksonUtil;
import com.example.lotterysystem.controller.param.CreatePrizeParam;
import com.example.lotterysystem.controller.param.PageParam;
import com.example.lotterysystem.controller.result.PrizeListResult;
import com.example.lotterysystem.service.IPictureService;
import com.example.lotterysystem.service.IPrizeCreateService;
import com.example.lotterysystem.service.dto.PrizeDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.example.lotterysystem.service.dto.PageListDTO;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
public class PrizeController {
    @Autowired
    private IPictureService pictureService;

    @Autowired
    private IPrizeCreateService prizeCreateService;

    /**
     * 图片上传
     * @param file
     * @return
     */
    @RequestMapping("/pic/upload")
    public String uploadPic(MultipartFile file) {
        return pictureService.savePicture(file);
    }

    /**
     * 创建商品
     * @param createPrizeParam
     * @param file
     * @return
     */
    @RequestMapping("prize/create")
    public CommonResult<Long> createPrize(@Validated @RequestPart("param") CreatePrizeParam createPrizeParam,
                                          @RequestPart("prizePic") MultipartFile file) {
        log.info("createPrize createPrizeParam: {}", JacksonUtil.writeValueAsString(createPrizeParam));
        return CommonResult.success(prizeCreateService.createPrize(createPrizeParam, file));
    }

    /**
     * 查询商品表
     * @param param
     * @return
     */
    @RequestMapping("/prize/find-list")
    public CommonResult<PrizeListResult> prizeFindList(PageParam param) {
        log.info("prizeFindList param :{}", param);
        PageListDTO<PrizeDTO> pageListDTO = prizeCreateService.findPrizeList(param);
        return CommonResult.success(convertToPrizeListResult(pageListDTO));
    }

    private PrizeListResult convertToPrizeListResult(PageListDTO<PrizeDTO> pageListDTO) {

        if (pageListDTO == null) throw new ControllerException(ControllerErrorCodeConstants.FIND_PRIZE_LIST_ERROR);
        PrizeListResult prizeListResult = new PrizeListResult();
        prizeListResult.setTotal(pageListDTO.getTotal());
        prizeListResult.setRecords(
                pageListDTO.getRecords().stream()
                        .map(prizeDTO -> {
                            PrizeListResult.PrizeInfo prizeInfo = new PrizeListResult.PrizeInfo();
                            prizeInfo.setPrizeId(prizeDTO.getId());
                            prizeInfo.setPrizeName(prizeDTO.getName());
                            prizeInfo.setDescription(prizeDTO.getDescription());
                            prizeInfo.setImageUrl(prizeDTO.getImageUrl());
                            prizeInfo.setPrice(prizeDTO.getPrice());
                            return prizeInfo;
                        }).collect(Collectors.toList())
        );
                return prizeListResult;
    }

}
